package com.software.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.lease.common.exception.LeaseException;
import com.software.lease.common.result.ResultCodeEnum;
import com.software.lease.model.entity.*;
import com.software.lease.model.enums.LeaseStatus;
import com.software.lease.web.admin.mapper.*;
import com.software.lease.web.admin.service.LeaseAgreementService;
import com.software.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.software.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    /**
     * 根据条件分页查询租约列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<AgreementVo> pageAgreement(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        IPage<AgreementVo> list = leaseAgreementMapper.pageAgreement(page,queryVo);
        return list;
    }

    /**
     * 根据id查询租约详细信息
     * @param id
     * @return
     */
    @Override
    public AgreementVo getDetailById(Long id) {
        //获取租约基本信息
        AgreementVo agreementVo = new AgreementVo();
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        BeanUtils.copyProperties(leaseAgreement,agreementVo);

        //获取签约公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(agreementVo.getApartmentId());
        agreementVo.setApartmentInfo(apartmentInfo);

        //获取签约房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(agreementVo.getRoomId());
        agreementVo.setRoomInfo(roomInfo);

        //获取支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(agreementVo.getPaymentTypeId());
        agreementVo.setPaymentType(paymentType);

        //获取租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(agreementVo.getLeaseTermId());
        agreementVo.setLeaseTerm(leaseTerm);
        return agreementVo;
    }

    /**
     * 修改或更新租约
     * @param leaseAgreement
     */
    @Override
    public void saveOrUpdateAgreement(LeaseAgreement leaseAgreement) {
        //获取id判断是更新还是修改
        Long id = leaseAgreement.getId();
        if(id == null){
            //新增
            //根据roomId获取租约表中该房间的记录
            Long roomId = leaseAgreement.getRoomId();
            LambdaQueryWrapper<LeaseAgreement> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(LeaseAgreement::getRoomId,roomId);
            wrapper.in(LeaseAgreement::getStatus, Arrays.asList(LeaseStatus.SIGNED,LeaseStatus.SIGNING,LeaseStatus.RENEWING));
            wrapper.eq(LeaseAgreement::getIsDeleted, 0);
            LeaseAgreement selectOne = leaseAgreementMapper.selectOne(wrapper);
            //如果租约表中存在该房间的租约,
            if(selectOne != null){
                //提示该房间已存在未结束的租约
                throw new LeaseException(ResultCodeEnum.PARAM_ERROR);
            }
            this.saveOrUpdate(leaseAgreement);

        }else{
            //修改直接修改
            this.saveOrUpdate(leaseAgreement);

        }
    }
}




